package com.ww.learn.uses

import java.io.File
import java.io.FileInputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream

fun copyFileWithMetadata(sourcePath: String, destPath: String) {
    val sourceFile = File(sourcePath)
    val destFile = File(destPath)

    // 复制文件内容
    sourceFile.inputStream().use { inputStream ->
        destFile.outputStream().use { outputStream ->
            inputStream.copyTo(outputStream)
        }
    }

    // 复制文件属性
    destFile.setLastModified(sourceFile.lastModified())
}

// 更复杂的多资源场景
fun processCompressedFile(filename: String) {
    FileInputStream(filename).use { fileInput ->
        ZipInputStream(fileInput).use { zipInput ->
            var entry: ZipEntry?
            while (zipInput.nextEntry.also { entry = it } != null) {
                println("处理条目：${entry!!.name}")
                // 处理每个压缩目录
                when {
                    entry!!.name.endsWith(".txt") -> {
                        zipInput.bufferedReader().use { reader ->
                            val content = reader.readText()
                            println("文本内容：${content.take(50)}")
                        }
                    }
                    entry!!.name.endsWith(".json") -> {
                        // JSON处理逻辑
                    }
                }
                zipInput.closeEntry()
            }
        }
    }
}